package rdatabase

import (
	"crypto/sha256"
	"fmt"
)

var pwdKey = ""

// SetPasswordKey 设置密码加密密钥
func SetPasswordKey(key string) {
	pwdKey = key
}

// GeneratePassword 通过明文密码，生成不可逆的密文密码
func GeneratePassword(username string, password string) string {
	if pwdKey == "" {
		if len(password) > 64 {
			password = password[:64]
		}
		return password
	}
	return fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint(username, "_", pwdKey, "_", password))))
}
